﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SynoManager.GUI
{
    public partial class ConnectionDialog : Form
    {
        public ConnectionDialog()
        {
            InitializeComponent();
        }

        private void ConnectionDialog_Load(object sender, EventArgs e)
        {
            addressTextBox.Text = Properties.Settings.Default.Address;
            usernameTextBox.Text = Properties.Settings.Default.Username;
            passwordTextBox.Text = GetStoredPassword();
            portNumericUpDown.Value = Properties.Settings.Default.Port;
            securePortNumericUpDown.Value = Properties.Settings.Default.SecurePort;
            preferSecureCheckBox.Checked = Properties.Settings.Default.PreferSecure;
            rememberCheckBox.Checked = Properties.Settings.Default.AutoConnect;
            ToggleAdvancedSettings(false);
        }

        public static string GetStoredPassword()
        {
            string encryptedPass = Properties.Settings.Default.Password;
            return PasswordHelper.Unprotect(encryptedPass);
        }

        public string Address
        {
            get { return addressTextBox.Text; }
            set { addressTextBox.Text = value; }
        }

        public string Username
        {
            get { return usernameTextBox.Text; }
            set { usernameTextBox.Text = value; }
        }

        public string Password
        {
            get { return passwordTextBox.Text;}
            set { passwordTextBox.Text = value;}
        }

        public int Port
        {
            get { return (int) portNumericUpDown.Value;}
            set { portNumericUpDown.Value = value; }
        }

        public int SecurePort
        {
            get { return (int) securePortNumericUpDown.Value;}
            set { securePortNumericUpDown.Value = value;}
        }

        public bool PreferSecure
        {
            get { return preferSecureCheckBox.Checked;}
            set { preferSecureCheckBox.Checked = value; }
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            errorProvider.Clear();
            bool errors = false;

            if (addressTextBox.Text == String.Empty)
            {
                errorProvider.SetError(addressTextBox,
                                       "Please enter the host name or IP Address of your Synology Device");
                errors = true;
            }

            if (usernameTextBox.Text == String.Empty)
            {
                errorProvider.SetError(usernameTextBox, "Please enter an user name");
                errors = true;
            }

            if (passwordTextBox.Text == String.Empty)
            {
                errorProvider.SetError(passwordTextBox, "Please enter a password");
                errors = true;
            }

            if (!errors)
            {
                if (rememberCheckBox.Checked)
                {
                    Properties.Settings.Default.AutoConnect = true;
                    Properties.Settings.Default.Address = Address;
                    Properties.Settings.Default.Username = Username;
                    Properties.Settings.Default.Password = PasswordHelper.Protect(Password);
                    Properties.Settings.Default.Port = Port;
                    Properties.Settings.Default.SecurePort = SecurePort;
                    Properties.Settings.Default.PreferSecure = PreferSecure;
                }
                else
                {
                    Properties.Settings.Default.AutoConnect = false;
                    Properties.Settings.Default.Address = String.Empty;
                    Properties.Settings.Default.Username = String.Empty;
                    Properties.Settings.Default.Password = String.Empty;
                    Properties.Settings.Default.Port = 5000;
                    Properties.Settings.Default.SecurePort = 5001;
                    Properties.Settings.Default.PreferSecure = false;
                }
                Properties.Settings.Default.Save();

                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        private void ToggleAdvancedSettings(bool showAdvancedSettingsPanel)
        {
            advancedSettingsPanel.Visible = showAdvancedSettingsPanel;
            if (showAdvancedSettingsPanel)
                Size = new Size(400, 344);
            else
                Size = new Size(400, 238);
            advancedButton.Visible = !showAdvancedSettingsPanel;
        }

        private void advancedButton_Click(object sender, EventArgs e)
        {
            ToggleAdvancedSettings(true);
        }
    }
}
